package com.hamll.search.listener;


import com.hamll.search.service.ItemSearchSerivce;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ItemListener {


    @Autowired
    private ItemSearchSerivce itemSearchSerivce;


    /**
     * 上架
     * 创建索引
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "up.queue"),
            exchange = @Exchange(name = "item.topic",type = ExchangeTypes.TOPIC),
            key = "item.up"
    ))
    public void receiveUpMsg(String itemId){
        log.info("接收到上架消息，msg={}",itemId);
        itemSearchSerivce.createIndex(itemId);
    }

    /**
     * 下架
     * 删除索引
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "down.queue"),
            exchange = @Exchange(name = "item.topic",type = ExchangeTypes.TOPIC),
            key = "item.down"
    ))
    public void receiveDownMsg(String itemId){
        log.info("接收到下架消息，msg={}",itemId);
        itemSearchSerivce.removeIndex(itemId);
    }
}
